﻿using MvvmFx.Common.ViewModels.Commands;

namespace MvvmFx.Common.ViewModels.Behaviors.ProcessManagement
{
    /// <summary>
    /// Command to save the process
    /// </summary>
    public class SaveProcessCommand : ViewModelCommand
    {
        private readonly ProcessService _processService;

        public SaveProcessCommand(ProcessService processService)
        {
            _processService = processService;

            processService.NewPropertyObserver()
                .AddHandler("CurrentStep", c => OnCanExecuteChanged());
        }

        protected override void OnExecute(object param)
        {
            _processService.Save();
        }

        protected override bool OnCanExecute(object param)
        {
            // Track/bubble changes to child ChangeService to parent
            //
            return _processService.State.IsModified || _processService.CurrentStep.ChangeService.IsModified;
        }
    }
}